package com.dzbh.cdsdriver.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.dzbh.cdsdriver.utils.HRetrofitNetHelper;

/**
 * fragment 基类
 *
 */
public class BaseFragment extends Fragment
{
    protected LayoutInflater inflater;
    protected View root;

    public ProgressDialog mDialog;
    public HRetrofitNetHelper retrofitNetHelper;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDialog = new ProgressDialog(getActivity());
        retrofitNetHelper = HRetrofitNetHelper.getInstance(getActivity());
    }

    /**
     * 重载方法
     * 
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return _
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        this.inflater = inflater;
        return super.onCreateView(inflater, container, savedInstanceState);
    }
    
    /**
     * activity 初始化操作，在此会依序调用initView()，initEvent()。之后可以进行一些实例化操作等
     */
    protected View init(int layout)
    {
        root = this.inflater.inflate(layout, null);
        initEvent();
        return root;
    }
    
    /**
     * 初始化控件 <br>
     * 
     */
    protected void initUI(View view){ }

    protected void loadData(){ }
    
    /**
     * 注册控件监听器
     */
    protected void initEvent(){ }
    
    /**
     * Eventbus 监听消息
     * 
     * @param obj _ void 最后更新时间：<date> 修改履历：<修改内容>
     */
    public void onEventMainThread(Object obj){ }
    
    /**
     * 接收服务器返回值
     * 
     * @param isSuccess 是否调用成功
     * @param errorCode 失败代码
     * @param json 数据
     * @param respCode 请求代码，用于区分请求 void 最后更新时间：<date> 修改履历：<修改内容>
     */
    protected void onPostResponse(boolean isSuccess, int errorCode, Object json, int respCode){ }

    /**
     * 显示toast
     *
     * @param resId
     */
    public void showToast(int resId) {
        Context context = getActivity();
        if (context !=null)
        Toast.makeText(context, this.getResources().getText(resId), Toast.LENGTH_SHORT).show();
    }
    
    /**
     * 显示toast
     * 
     * @param text
     */
    public void showToast(String text){
        Context context = getActivity();
        if (context !=null)
        Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
}
